package com.learning.springboot.springdataJpa;

import com.learning.springboot.Thymeleaf.entity.Person;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by td on 2017/9/14.
 */
public interface PersonRepository extends JpaRepository<Person,Long>{

    List<Person> findFirst10ByName(String name);

    @Query("select p from Person p where p.name = ?1")
    List<Person> findByName(String name);


    @Query("select p from Person p where p.age = :age")
    List<Person> findByAddress (@Param("age") int age);

    // 进行更新, 更新一般是先查询后更新
    @Modifying
    @Transactional  // 一般在service层加上这个
    @Query ("update Person p set p.name=?1")
    int setname(String name);

    // 使用排序
    List<Person> findByName(String name, Sort sort);

    // 使用分业
    List<Person> findByName(String name, Pageable pageable);

    @RestResource(path = "/person",rel = "person")
    @Query("select p from Person p where p.name = :name")
    List<Person> findByPerson(@Param("name") String name);


   // Person savePerson(Person person);


  //  int deleteByName(String name);

    // Page<Person> findbyCustom()







}
